package com.shuang.controller;

import com.shuang.pojo.PageResult;
import com.shuang.pojo.Result;
import com.shuang.pojo.Student;
import com.shuang.pojo.StudentQueryParam;
import com.shuang.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /*条件分页查询*/
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam){
        log.info("条件分页查询参数：{}", studentQueryParam);
        PageResult<Student> studentPageResult = studentService.page(studentQueryParam);
        return Result.success(studentPageResult);
    }

    /*添加学员*/
    @PostMapping
    public Result save(@RequestBody Student student){
        log.info("添加学员：{}", student);
        studentService.save(student);
        return Result.success();
    }

    /*根据id查询学员 -> 查询回显*/
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id = {}查询学员信息", id);
        Student student = studentService.getInfo(id);
        return Result.success(student);
    }

    /*更新学员信息*/
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("更新学员信息：{}", student);
        studentService.update(student);
        return Result.success();
    }


    /*删除学员*/
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("根据id = {}批量删除学员", ids);
        studentService.delete(ids);
        return Result.success();
    }

    /*违纪处理*/
    @PutMapping("/violation/{id}/{score}")
    public Result violate(@PathVariable Integer id, @PathVariable String score){

        //对传过来的score做判断处理
        if (score.equals("null") || score.equals("")){
            score = "0";
        }

        log.info("学员id = {}，违纪扣分：{}", id, score);
        studentService.violate(id, score);
        return Result.success();
    }

}
